package org.example.atry.myservlet;

import jakarta.servlet.GenericServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.atry.service.agricultuerservicec;
import org.example.atry.service.impl.serviced;
import org.example.atry.util.Users;
import org.json.JSONObject;


import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Parameter;
import java.util.List;
@WebServlet(urlPatterns = {"/pageallinfo"})
public class pageallinfo extends GenericServlet {
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        // 设置允许跨域的头部信息
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Max-Age", "3600");
        agricultuerservicec serviced=new serviced();
        String cas=request.getParameter("cu");
//        System.out.println(cas);
int current=1;
try {

    current = Integer.parseInt(cas);
    if (current <= 0) {
        current = 1;
    }
} catch (Exception e) {
   current=1;
}
int pageSize=10;
int totalcounts=serviced.getTotal();
int totalpages=(int) Math.ceil((double)totalcounts/pageSize);
        //System.out.println(totalpages);
if(current>totalpages){
    current=totalpages;
}
List<Users> user=serviced.pageall(current, pageSize);
        JSONObject obj=new JSONObject();
        obj.put("totalpages",totalpages);
        obj.put("current",current);
       //obj.put("pageSize",pageSize);
        obj.put("user",user);

        PrintWriter out=response.getWriter();
        out.print(obj);
        out.flush();
/*
        request.setAttribute("user",user);
        request.setAttribute("current",current);
        request.setAttribute("pageSize",pageSize);
        request.setAttribute("totalpages",totalpages);



        request.getRequestDispatcher("queryall.jsp").forward(request, response);

 */
    }

}
